package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.util.AliyunOSSperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class UploadController {

    @Autowired
    private AliyunOSSperator aliyunOSSperator;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        String sub = filename.substring(filename.lastIndexOf("."), filename.length());
        String uniqueFileName = UUID.randomUUID().toString().replace("-", "") + sub;
        String upload = aliyunOSSperator.upload(file.getBytes(), uniqueFileName);
        return Result.success(upload);
    }
}
